package org.gwtEss.client.border;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;

public abstract class Border extends Composite{
	abstract String getTopLeftImage();
	abstract String getTopMiddleImage();
	abstract String getTopRightImage();
	
	abstract String getCenterLeftImage();
	abstract String getCenterRightImage();

	abstract String getBottomLeftImage();
	abstract String getBottomMiddleImage();
	abstract String getBottomRightImage();
	Grid mainPanel = new Grid(3,3);
	public Border() {
		init();
	}
	public Border(Widget w) {
		init();
		mainPanel.setWidget(1, 1, w);
	}
	@Override
	public void setWidget(Widget w) {
		mainPanel.remove(w);
		mainPanel.setWidget(1, 1, w);
	}
	private void init() {
		mainPanel.setBorderWidth(0);
		mainPanel.setCellPadding(0);
		mainPanel.setCellSpacing(0);
		mainPanel.setWidget(0, 0, new Image(getTopLeftImage()));
		mainPanel.setWidget(0, 1, new Image(getTopMiddleImage()){
			@Override
			protected void onLoad() {
				setSize("100%", getOffsetHeight() + "px");
				super.onLoad();
			}
		});
		mainPanel.setWidget(0, 2, new Image(getTopRightImage()));

		mainPanel.setWidget(1, 0, new ImageWithOnLaod(getCenterLeftImage()){

			@Override
			public void doOnLoad() {
				System.out.println("onlad");
			}
			
		});
		/*
		mainPanel.setWidget(1, 0, new Image(getCenterLeftImage()){
			@Override
			protected void onLoad() {
				setSize(getOffsetWidth() + "px", "100%");
				super.onLoad();
			}
		});*/
		mainPanel.setWidget(1, 2, new Image(getCenterRightImage()){
			@Override
			protected void onLoad() {
				setSize(getOffsetWidth() + "px", "100%");
				super.onLoad();
			}
		});

		mainPanel.setWidget(2, 0, new Image(getBottomLeftImage()));
		mainPanel.setWidget(2, 1, new Image(getBottomMiddleImage()){
			@Override
			protected void onLoad() {
				setSize("100%", getOffsetHeight() + "px");
				super.onLoad();
			}
		});
		mainPanel.setWidget(2, 2, new Image(getBottomRightImage()));
		
		CellFormatter cellFormatter = mainPanel.getCellFormatter();
		cellFormatter.setWidth(1, 1, "100%");
		cellFormatter.setHeight(1, 1, "100%");
		cellFormatter.setVerticalAlignment(1, 1, HasVerticalAlignment.ALIGN_TOP);	
		initWidget(mainPanel);
	}
	
	public abstract class ImageWithOnLaod extends Image{
		public ImageWithOnLaod(String centerLeftImage) {
			super(centerLeftImage);
		}
		
		@Override
		protected void onAttach() {
			doOnLoad();
			super.onLoad();
		}
		public abstract void doOnLoad();
	}
}
